package com.zzc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzc.common.JsonResult;
import com.zzc.common.TableResult;
import com.zzc.mapper.AdminUserMapper;
import com.zzc.mapper.RoleMapper;
import com.zzc.mapper.UserRoleMapper;
import com.zzc.pojo.AdminUser;
import com.zzc.pojo.Role;
import com.zzc.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhouzhicheng
 * @version 1.0  2020/12/29
 */
@Service("adminUserService")
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper  userRoleMapper;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public AdminUser selectByPrimaryKey(Integer id) {
        return adminUserMapper.selectByPrimaryKey(id);
    }


    @Override
    public JsonResult doLogin(String email, String password) {
        AdminUser adminUser= adminUserMapper.doLogin(email,password);
       if (adminUser!=null){
           return JsonResult.getResultSuccess(adminUser);
       }
        return JsonResult.getResultFailed("登录失败，用户名或密码错误！");
    }

    @Override
    public TableResult getUserList(String realName,String email,Integer status,Integer page,Integer limit) {
        //设置分页条件
        PageHelper.startPage(page,limit);
        List<AdminUser> userList = adminUserMapper.getUserList(realName,email,status);
        //分页信息
        PageInfo<AdminUser> pageInfo = new PageInfo<>(userList);
        //返回TableResult格式数据
        return new TableResult(0,null,Long.valueOf(pageInfo.getTotal()).intValue(),pageInfo.getList());

    }

    @Override
    public JsonResult updateByPrimaryKeySelective(AdminUser adminUser) {
        if (adminUser==null){
            return JsonResult.getResultFailed("参数有误！");
        }
        System.out.println(adminUser);
        return adminUserMapper.updateByPrimaryKeySelective(adminUser)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("修改失败！");
    }

    @Override
    public JsonResult deleteUserById(Integer[] ids) {
        if (ids==null||ids.length<=0){
            return JsonResult.getResultFailed("参数有误！");
        }
        System.out.println(ids);
        return adminUserMapper.deleteUserById(ids) >0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("删除失败！");
    }

    @Override
    public JsonResult insertUser(AdminUser user) {
        if (user==null){
            return JsonResult.getResultFailed("参数有误！");
        }
        return adminUserMapper.insertSelective(user)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("添加失败！");
    }

    @Override
    public List<Integer> getUserRole(Integer userId) {
        return adminUserMapper.getUserRole(userId);
    }

    @Override
    public List<Role> getRoles() {
        return roleMapper.getRoleList(null);
    }

    @Override
    public JsonResult updateUserRole(Integer userId, Integer[] roleIds) {
        try {
            //先移除该用户的所有role
            userRoleMapper.deleteByUserID(userId);
            //给用户批量添加权限
            for (Integer roleId : roleIds) {
                userRoleMapper.addUserRole(userId,roleId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getResultFailed("修改失败！");
        }
        return JsonResult.getResultSuccess();
    }

}
